package com.example.javapoint.sync;

/**
 * 不使用同步方法来操作同一个资源会有问题
 */
class Table {
    public void printTable(int n) {
        for (int i = 1; i < 100; i++) {
            System.out.println( n * i);
        }
    }
}

class MyThread1 extends Thread{
    Table t;
    MyThread1(Table t) {
        this.t = t;
    }

    @Override
    public void run() {
        t.printTable(5);
        try {
            Thread.sleep(400);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class MyThread2 extends Thread {
    Table t;
    MyThread2(Table t) {
        this.t = t;
    }

    @Override
    public void run() {
        t.printTable(100);
        try {
            Thread.sleep(400);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

public class TestSynchronization1 {
    public static void main(String[] args) {
        Table t = new Table();
        MyThread1 myThread1 = new MyThread1(t);
        MyThread2 myThread2 = new MyThread2(t);

        myThread1.start();
        myThread2.start();
    }
}
